package com.codedancer.campus.client.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codedancer.campus.client.async.ChatMessageAsync;
import com.codedancer.campus.client.service.IChatMessageService;
import com.codedancer.campus.client.service.bo.chatMessage.ChatMessageListPageSearchBo;
import com.codedancer.campus.client.service.bo.chatMessage.ChatMessagePageSearchBo;
import com.codedancer.campus.client.service.vo.chatMessage.ChatRoomListVo;
import com.codedancer.campus.client.service.vo.chatMessage.ChatMessageVo;
import com.codedancer.campus.common.exception.BusinessException;
import com.codedancer.campus.mapper.ChatMessageMapper;
import com.codedancer.campus.mapper.ChatRoomMapper;
import com.codedancer.campus.po.ChatMessage;
import com.codedancer.campus.po.ChatRoom;
import com.codedancer.campus.qo.chatMessage.ChatMessageSearchQo;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class ChatMessageServiceImpl implements IChatMessageService {

    @Autowired
    private ChatMessageMapper chatMessageMapper;
    @Autowired
    private ChatRoomMapper chatRoomMapper;
    @Autowired
    private ChatMessageAsync chatMessageAsync;

    /**
     * 分页查询聊天记录
     *
     * @param chatMessagePageSearchBo
     * @return
     */
    @Override
    public IPage<ChatMessageVo> page(ChatMessagePageSearchBo chatMessagePageSearchBo) {
        if (ObjectUtil.isEmpty(chatMessagePageSearchBo.getSendUserUuid()) || ObjectUtil.isEmpty(
            chatMessagePageSearchBo.getReceiveUserUuid())) {
            throw new BusinessException("发送者或接收者uuid为空");
        }
        ChatMessageSearchQo chatMessageSearchQo = BeanUtil.copyProperties(chatMessagePageSearchBo,
            ChatMessageSearchQo.class);
        IPage<ChatMessage> chatMessageIPage = chatMessageMapper.pageHistory(
            new Page<ChatMessage>(chatMessagePageSearchBo.getPage(),
                chatMessagePageSearchBo.getSize()), chatMessageSearchQo);
        IPage<ChatMessageVo> chatMessageVoIPage = new Page<>();
        List<ChatMessageVo> chatMessageVos;
        chatMessageVos = chatMessageIPage.getRecords().stream().map(chatMessage -> {
            ChatMessageVo chatMessageVo = BeanUtil.copyProperties(chatMessage, ChatMessageVo.class);
            chatMessageVo.setSendUserUuid(chatMessage.getSendUser().getUuid());
            chatMessageVo.setSendUserName(chatMessage.getSendUser().getNickName());
            chatMessageVo.setSendUserAvatar(chatMessage.getSendUser().getAvatar());
            chatMessageVo.setReceiveUserUuid(chatMessage.getReceiveUser().getUuid());
            chatMessageVo.setReceiveUserName(chatMessage.getReceiveUser().getNickName());
            chatMessageVo.setReceiveUserAvatar(chatMessage.getReceiveUser().getAvatar());
            return chatMessageVo;
        }).collect(Collectors.toList());
        chatMessageVoIPage.setTotal(chatMessageIPage.getTotal());
        chatMessageVoIPage.setRecords(chatMessageVos);
        // 将该用户收到的消息全部设置为已读
        chatMessageAsync.setMessageReaded(chatMessagePageSearchBo.getCurrentUserUuid(),chatMessagePageSearchBo.getReceiveUserUuid());
        return chatMessageVoIPage;
    }

    /**
     * 分页查询聊天列表
     *
     * @param chatMessageListPageSearchBo
     * @return
     */
    @Override
    public IPage<ChatRoomListVo> pageList(
        ChatMessageListPageSearchBo chatMessageListPageSearchBo) {
        ChatMessageSearchQo chatMessageSearchQo = BeanUtil.copyProperties(
            chatMessageListPageSearchBo,
            ChatMessageSearchQo.class);
        IPage<ChatRoom> chatMessageUserIPage = chatRoomMapper.pageList(
            new Page<ChatMessage>(chatMessageListPageSearchBo.getPage(),
                chatMessageListPageSearchBo.getSize()), chatMessageSearchQo);
        IPage<ChatRoomListVo> chatMessageListVoIPage = new Page<>();
        List<ChatRoomListVo> chatRoomListVos;
        chatRoomListVos = chatMessageUserIPage.getRecords().stream().map(chatRoom -> {
            ChatRoomListVo chatRoomListVo = new ChatRoomListVo();
            // 判断房主是否是当前登录人uuid
            if (chatMessageListPageSearchBo.getCurrentUserUuid().equals(chatRoom.getHouseOwnerUuid())){
                // 如果说是，就需要设置为另外一个用户信息
                chatRoomListVo.setSendUserUuid(chatRoom.getChatUserUuid());
                chatRoomListVo.setSendUserName(chatRoom.getChatUser().getNickName());
                chatRoomListVo.setSignature(chatRoom.getChatUser().getSignature());
                chatRoomListVo.setSendUserAvatar(chatRoom.getChatUser().getAvatar());
                // 两个用户之间是否存在未读消息
                Boolean isExitsNoRead = chatMessageMapper.isExitsNoReadBySendUuidAndReceiveUuid(chatRoom.getChatUserUuid(),chatMessageListPageSearchBo.getCurrentUserUuid());
                chatRoomListVo.setIsExitsNoRead(isExitsNoRead);
            }else {
                // 否则设置为房主信息
                chatRoomListVo.setSendUserUuid(chatRoom.getHouseOwnerUuid());
                chatRoomListVo.setSendUserName(chatRoom.getHouseOwnerUser().getNickName());
                chatRoomListVo.setSignature(chatRoom.getHouseOwnerUser().getSignature());
                chatRoomListVo.setSendUserAvatar(chatRoom.getHouseOwnerUser().getAvatar());
                Boolean isExitsNoRead = chatMessageMapper.isExitsNoReadBySendUuidAndReceiveUuid(chatRoom.getHouseOwnerUuid(),chatMessageListPageSearchBo.getCurrentUserUuid());
                chatRoomListVo.setIsExitsNoRead(isExitsNoRead);
            }
            return chatRoomListVo;
        }).collect(Collectors.toList());
        chatMessageListVoIPage.setRecords(chatRoomListVos);
        chatMessageListVoIPage.setTotal(chatMessageUserIPage.getTotal());
        return chatMessageListVoIPage;
    }

    /**
     * 是否存在未读消息
     * @param currentLoginUuid
     * @return
     */
    @Override
    public Boolean isExitsNoRead(String currentLoginUuid) {
        return chatMessageMapper.isExitsNoRead(currentLoginUuid);
    }
}
